//
//  ASTabcontainerDemo.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/2/12.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import Eelay
import soapp

class FixHead: ASSizeRangeCell{
    override func addLayoutRules() {
        backgroundColor = .red
    }
    override func sizeRange() -> ASSizeRange {
        return ASSizeRange(min: CGSize(width: Swidth, height: 40), max: CGSize(width: Swidth, height: 40))

    }
}

class ASTabcontainerDemo: ASCollectionContainer {
    override var sections: TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cellID":"adv","list":[
                        ["img":__images.one_str],
                        ["img":__images.one_str],
                        ["img":__images.one_str],
                    ]]
                ],
                [
                    ["header":"YES"]
                ],
            ]
            return sec
        }
    }
    
    override var selector: TP.selector{
        get{
            let sel:TP.selector = [
                "<section1>":Cell.self,
                "cellID<adv>":AdvCell.self,
                "header<YES>":FixHead.self
            ]
            return sel
        }
    }
    

    override func viewDidLoad() {
        page_load_section = "section1"
        self.content = ASTermsContainer.as_collection(title: "test", emptyMsg: "没有相关内容".loc, net: JoTask().post.url("http://apijson.cn:8080/get/").apijson(["[]":["Moment":[:]]]), params: nil, node: "[]", cellType: Cell.self, detailType: nil, test: false)
//        self.content = ASTermsContainer.as_table(
//            title: "祥鹏旅游",
//            emptyMsg:"没有相关内容",
//            net: JoTask().log.post.url("http://iwe.doncheng.cn/api/store/storelist").log,
//            params:nil,
//            node: "list",
//            cellType: Cell.self,
//            cellHeigth: nil,
//            detailType: nil,
//            test: false
//        )
        super.viewDidLoad()
        
//        self.paginator.controlType = .UIRefreshControl
        self.top_style = .default
        reload_page()
        print("=====|\(collectionNode.model)")
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


extension ASTabcontainerDemo{
    
    
    
    class AdvCell:JoCellNode,EeActionDelegate{
        func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
            active(ID: actionID, object: data)
        }
        
        class ImgView: JoTouchNode {
            let imgNode = JoImageNode()
            
            override func specGetter() -> _Spec {
                return .stack([], childs: [
                    imgNode[_shrink(1),_grow(1)]
                    ])
            }
            
            override func mo_model_reload() {
                imgNode.img_url = mo.model["img",""]
            }
            
        }
        
        let imgsV = JoLoopView<ImgView>()
        
        override func addLayoutRules() {
            
//            mf.spec = .ratio(ratio: 0.5, child: imgsV[], rules:[])
            imgsV.deletate = self
        }
        
        override func specGetter() -> _Spec {
            return .stack([], childs: [
                imgsV[Swidth,"\(Swidth*0.5)"]
                ])
        }
        override func mo_model_reload() {
//
//            if mo.model["__load","NO"] == "NO"
//            {
                imgsV.datas = DataNode.path(node: "list", mo.model, value: NSMutableArray())
//            }
            mo.model["__load"] = "YES"
        }
        
        
        
    }
    

    
    class Cell:JoCellNode{
        
        let imgV = JoImageNode()
        let titleL = ASTextNode()
        let infoL = ASTextNode()
        let timeL = ASTextNode()
        
        override func addLayoutRules() {
            
            infoL.maximumNumberOfLines = 1
            self.backgroundColor = .white
            
            separatorHeight = TP.separatorHeight
            

        }
        override func specGetter() -> _Spec {
            return .inset([],[8], child:.stack(["h|10",_shrink(1),_nowrap], childs: [
                imgV["\(90)",120],
                .stack(["V|10",_shrink(1)], childs: [
                    titleL[_shrink(1)],
                    infoL[_shrink(1)]
                    ])
                ]))
        }
        
        override func mo_model_reload() {

//            print("====UU|\(mo.model)")
            imgV.img_url = mo.model["Moment.pictureList.0",""]
//            imgV.url = URL(string: mo.model["thumb",""])
//            imgV.url = URL(string:"http://img4.duitang.com/uploads/blog/201409/06/20140906181244_WAjKB.gif")
            titleL.attributedText = mo.model["Moment.content",""].ui.cl_222.bfont16.lineSpace(5).astring
            infoL.attributedText = mo.model["Moment.date",""].ui.cl_666.font14.lineSpace(5).astring
            
        }
        
        
        deinit {
        }
    }
    
}







